
import * as constants from '../../../../common/Constants';
import * as network_utils from '../../../../http/NetworkUtils';
import * as regexUtil from '../../../../utils/RegexUtils';

import { $Toptips } from '../../../../components/CompRouter';

var app=getApp()
Page({
    data: {
        errorMsg: '',
    },

    onLoad:function(){
      this.setData({
        canSearchStores:app.globalData.userData.permConfig.canSearchStores(),
        canSearchProfit:app.globalData.userData.permConfig.canSearchProfit(),
      })
    },

    onNext: function (e) {
        if (!this._checkForm(e.detail.value.nameInput)) {
            return;
        }

        this._requestAddClerks(e);
    },

    _checkForm(userId) {
        let errorMsg = regexUtil.regexTypeWithMsg(userId, constants.RegexTypes.USER_ID);
        if (errorMsg) {
            $Toptips.error({text: errorMsg});
            // this.setData({
            //     errorMsg: result
            // });
            return false;
        }
        // this.setData({
        //     errorMsg: ''
        // });
        return true;
    },
    
    _requestAddClerks: function (e) {
      let permissionConf = [];
      e.detail.value.fetchBillDocPerm && permissionConf.push("fetchBillDocPerm")
      e.detail.value.searchStorePerm && permissionConf.push("searchStorePerm")
      e.detail.value.searchProfitPerm && permissionConf.push("searchProfitPerm")
        let params = {
          userId: e.detail.value.nameInput,
          permissionConf:JSON.stringify(permissionConf)
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_SALESMAN_ADD, params, {
            doHttpSucess: (response)=> {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    this._onAddSuccess(response,params.userId);
                    return true;
                }
                return false;
            }
        }).send();
    },
    
    _onAddSuccess: function (result,userId) {
        let qrUrl = encodeURIComponent(result["qrUrl"]);
        wx.redirectTo({
          url: '../inviteClerks/inviteClerks?userId='+ userId + '&qrUrl=' + qrUrl,
        });
    }
});